/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generation.propery;

import com.farmcode.base.dao.DAOException;
import com.farmcode.base.manager.FacadeException;
import com.farmcode.console.api.business.ProjectManager;
import com.farmcode.console.api.business.custom.ExtPropertyManager;
import com.farmcode.console.api.model.Property;
import com.farmcode.console.api.util.Constants;
import com.farmcode.web.generation.CodeGeneratorBackingBean;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Dependent;
import javax.inject.Named;


/**
 *
 * @author Oscar Ivan Prado
 */
@Named(value = "variableBackingBean")
@Dependent
public class VariableBackingBean extends BaseProperty {

    private Property newProperty;
    private List<Property> properties;
    private List<Property> variables;
    @EJB
    private ExtPropertyManager extPropertyManager;    
    
    public static final String NEW_PROPERTY = VariableBackingBean.class.getName() + ".newProperty";
    public static final String PROPERTIES_SESSION_NAME=VariableBackingBean.class.getName()+"properties";
    public static final String VARIABLES_SESSION_NAME=VariableBackingBean.class.getName()+"variables";
    
    /** Creates a new instance of VariableBackingBean */
    public VariableBackingBean() {
    }

    public void onCreate() throws FacadeException{
        Property property = getNewProperty();
        property.setType(Constants.VARIABLE);
        property.setProject(getProject());
        propertyManager.create(property);
        setNewProperty(new Property());
        getSession().setValue(VARIABLES_SESSION_NAME, null);
    }
    
    public void onRemove(Property property) throws FacadeException{
        propertyManager.remove(property);
        getSession().setValue(VARIABLES_SESSION_NAME, null);
    }
    
    public String onModfy(Property property) throws FacadeException{
        propertyManager.store(property);   
        return null;
    }
    
    /**
     * @return the property
     */
    public Property getNewProperty() {
        if(getSession().getValue(NEW_PROPERTY) != null){
            newProperty = getSession().getValue(NEW_PROPERTY);
        }else{
            setNewProperty(new Property());
        }
        return newProperty;
    }

    /**
     * @param property the property to set
     */
    public void setNewProperty(Property property) {
        this.newProperty = property;
        getSession().setValue(NEW_PROPERTY, property);
    }

    public List<Property> getProperties() throws FacadeException, DAOException{
         if( getSession().getValue(PROPERTIES_SESSION_NAME)==null){
            setProperties((List<Property>)extPropertyManager.getProjectParameters(getProject()));
         }else
         {
             setProperties((List<Property>)getSession().getValue(PROPERTIES_SESSION_NAME));
         }
            return properties;       
    }

    public void setProperties(List<Property> properties) {
        this.properties = properties;
        getSession().setValue(PROPERTIES_SESSION_NAME, properties);
    }

    public List<Property> getVariables() throws DAOException {           
         if( getSession().getValue(VARIABLES_SESSION_NAME)==null){
            setVariables((List<Property>)extPropertyManager.getProjectVariables(getProject()));
         }else
         {
             setVariables((List<Property>)getSession().getValue(VARIABLES_SESSION_NAME));
         }   
            return variables;
    }

    public void setVariables(List<Property> variables) {
        this.variables = variables;
         getSession().setValue(VARIABLES_SESSION_NAME, variables);
    }
//    @Override
//    protected Project getProject() {
//        try {
//            super.setProject(projectManager.find().get(0));
//        } catch (FacadeException ex) {
//            Logger.getLogger(VariableBackingBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return super.getProject();
//    }
    
}
